package com.cxhc.demo1005;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

import androidx.annotation.RequiresApi;

public class MyScrollView extends HorizontalScrollView {
    public MyScrollView(Context context) {
        this(context, null);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    int width = 0;

//    @Override
//    protected void onFinishInflate() {
//        super.onFinishInflate();
//        width = getMeasuredWidth();
//        Log.e(TAG, "onFinishInflate: " +width );
//    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        measure(0,0);
        width = getMeasuredWidth();
//        width = getMa
        Log.e(TAG, "onMeasure: " + width);
    }

    boolean isHandleTouch = true;//是否响应滑动事件
    int handleState = 0;
    int LEFT = -1;
    int Right = 1;
    int Mid = 0;

    @RequiresApi(23)
    private void init() {
        setOnScrollChangeListener(new OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollX-oldScrollX>0&&scrollX>=1220){
                    //滑动到有边界了
                    isHandleTouch = false;
                    handleState = Right;
//                    requestDisallowInterceptTouchEvent(false);
                }else if (scrollX-oldScrollX<0&&scrollX<=0){
                    //滑动到左边界了
                    isHandleTouch = false;
                    handleState = LEFT;
//                    requestDisallowInterceptTouchEvent(false);
                }else {
                    //中间滑动
                    isHandleTouch =true;
                    handleState = Mid;
//                    requestDisallowInterceptTouchEvent(true);
                }
                Log.e(TAG, "onScrollChange: " +isHandleTouch+"---,"+oldScrollX +"----," +scrollX);
            }


        });
    }

    @RequiresApi(23)
    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }


    public static final String TAG = MyScrollView.class.getSimpleName();


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (MotionEvent.ACTION_DOWN == ev.getAction()) {
            Log.e(TAG, "onTouchEvent: ACTION_DOWN" + isHandleTouch);
            if (handleState == LEFT || handleState == Right) {
                return false;
            } else
                return true;
        }
        return super.onTouchEvent(ev);
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        return true;
//    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        requestDisallowInterceptTouchEvent(true);
//        return super.dispatchTouchEvent(ev);
//    }
}
